package de.se.ludoapp.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import de.se.ludoapp.LudoApplication;
import de.se.ludoapp.R;
import de.se.ludoapp.activity.task.DiceTask;
import de.se.ludoapp.activity.task.LeaveRunningGameTask;
import de.se.ludoapp.activity.task.UpdateGameFieldTask;
import de.se.ludoapp.gaming.Game;
import de.se.ludoapp.gaming.PlayerType;

/**
 * Game Activity, shows the gameboard
 * 
 * @author Marlen Rotert
 * 
 */
public class GameActivity extends Activity {

	private LudoApplication ludoApp;

	private Handler handler;

	private Runnable runnable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Initialise Stub LudoServer
		ludoApp = (LudoApplication) this.getApplication();

		// View
		setContentView(R.layout.game);

		// initialise GameBoard
		if (ludoApp.getCurrentGame().getGameField() == null) {
			new UpdateGameFieldTask(ludoApp, GameActivity.this).execute();
		} else {
			drawGameBoard();
		}

		// Dice Button
		final Button buttonDice = (Button) findViewById(R.id.buttonDice);

		// Activated Button when only your turn
		if (ludoApp.getCurrentGame().getUpNext() == ludoApp.getlogedinUser()
				.getSessionId()) {
			buttonDice.setEnabled(true);
		} else {
			buttonDice.setEnabled(false);
		}

		buttonDice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				buttonDice.setEnabled(false);
				new DiceTask(ludoApp, GameActivity.this).execute();
			}

		});

		ImageView imageViewDice = (ImageView) findViewById(R.id.imageViewDice);
		imageViewDice.setVisibility(View.GONE);

		// Leave Game Button
		final Button buttonLeaveGame = (Button) findViewById(R.id.buttonLeaveGame);
		buttonLeaveGame.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				handler.removeCallbacks(runnable);
				buttonLeaveGame.setEnabled(false);
				new LeaveRunningGameTask(ludoApp, GameActivity.this).execute();
			}

		});

		// update gamefield, delay 1 sek
		handler = new Handler();
		runnable = new Runnable() {
			public void run() {
				new UpdateGameFieldTask(ludoApp, GameActivity.this).execute();
				handler.postDelayed(this, 1000);
			}
		};
		handler.postDelayed(runnable, 100);

	}

	@Override
	protected void onPause() {
		super.onPause();
		handler.removeCallbacks(runnable);
	}

	/**
	 * Update GameBoard
	 */
	public void drawGameBoard() {

		// clear GameBoard
		int[] allPositions = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
				15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
				31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
				47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
				63, 64, 65, 66, 67, 68, 69, 70, 71, 72 };

		for (int i = 0; i < allPositions.length; i++) {
			setImageOnPosition(allPositions[i], null);
		}

		// set player on gameboard
		Game game = ludoApp.getCurrentGame();
		ArrayList<Integer> gameField = game.getGameField();

		// Yellow characters
		TextView textViewPlayerYellow = (TextView) findViewById(R.id.textViewPlayer1);
		if (game.getNameYellowPlayer() != "") {
			textViewPlayerYellow.setText(game.getNameYellowPlayer());
			// characterOne Yellow
			setImageOnPosition(gameField.get(0),
					getPlayerImageByType(PlayerType.PLAYER_YELLOW));
			// characterTwo Yellow
			setImageOnPosition(gameField.get(1),
					getPlayerImageByType(PlayerType.PLAYER_YELLOW));
			// characterThree Yellow
			setImageOnPosition(gameField.get(2),
					getPlayerImageByType(PlayerType.PLAYER_YELLOW));
			// characterFour Yellow
			setImageOnPosition(gameField.get(3),
					getPlayerImageByType(PlayerType.PLAYER_YELLOW));

		} else {
			textViewPlayerYellow.setVisibility(View.GONE);
		}
		// Red characters
		TextView textViewPlayerRed = (TextView) findViewById(R.id.textViewPlayer2);
		if (game.getNameRedPlayer() != "") {
			textViewPlayerRed.setText(game.getNameRedPlayer());
			// characterOne Red
			setImageOnPosition(gameField.get(4),
					getPlayerImageByType(PlayerType.PLAYER_RED));
			// characterTwo Red
			setImageOnPosition(gameField.get(5),
					getPlayerImageByType(PlayerType.PLAYER_RED));
			// characterThree Red
			setImageOnPosition(gameField.get(6),
					getPlayerImageByType(PlayerType.PLAYER_RED));
			// characterFour Red
			setImageOnPosition(gameField.get(7),
					getPlayerImageByType(PlayerType.PLAYER_RED));
		} else {
			textViewPlayerRed.setVisibility(View.GONE);
		}
		// Green characters
		TextView textViewPlayerGreen = (TextView) findViewById(R.id.textViewPlayer3);
		if (game.getNameGreenPlayer() != "") {
			textViewPlayerGreen.setText(game.getNameGreenPlayer());
			// characterOne Green
			setImageOnPosition(gameField.get(8),
					getPlayerImageByType(PlayerType.PLAYER_GREEN));
			// characterTwo Green
			setImageOnPosition(gameField.get(9),
					getPlayerImageByType(PlayerType.PLAYER_GREEN));
			// characterThree Green
			setImageOnPosition(gameField.get(10),
					getPlayerImageByType(PlayerType.PLAYER_GREEN));
			// characterFour Green
			setImageOnPosition(gameField.get(11),
					getPlayerImageByType(PlayerType.PLAYER_GREEN));
		} else {
			textViewPlayerGreen.setVisibility(View.GONE);
		}
		// Blue characters
		TextView textViewPlayerBlue = (TextView) findViewById(R.id.textViewPlayer4);
		if (game.getNameBluePlayer() != "") {
			textViewPlayerBlue.setText(game.getNameBluePlayer());
			// characterOne Blue
			setImageOnPosition(gameField.get(12),
					getPlayerImageByType(PlayerType.PLAYER_BLUE));
			// characterTwo Blue
			setImageOnPosition(gameField.get(13),
					getPlayerImageByType(PlayerType.PLAYER_BLUE));
			// characterThree Blue
			setImageOnPosition(gameField.get(14),
					getPlayerImageByType(PlayerType.PLAYER_BLUE));
			// characterFour Blue
			setImageOnPosition(gameField.get(15),
					getPlayerImageByType(PlayerType.PLAYER_BLUE));
		} else {
			textViewPlayerBlue.setVisibility(View.GONE);
		}

		// Dice Button
		final Button buttonDice = (Button) findViewById(R.id.buttonDice);
		if (game.getUpNext() == ludoApp.getlogedinUser().getSessionId()) {
			buttonDice.setEnabled(true);
		} else {
			buttonDice.setEnabled(false);
		}
	}

	public ImageView setImageOnPosition(Integer characterPosition,
			Drawable image) {
		ImageView fieldPlayer;
		fieldPlayer = (ImageView) findViewById(getImageFieldId(characterPosition));
		fieldPlayer.setImageDrawable(image);
		// Man darf nicht klicken
		fieldPlayer.setOnClickListener(null);
		return fieldPlayer;
	}

	/**
	 * get dynamic ImageFieldRessourceId from all field of playground
	 * 
	 * @param position
	 * @return
	 */
	private int getImageFieldId(Integer position) {
		String imageField = "imagePlaceholder";
		return getResources().getIdentifier(
				imageField + String.valueOf(position), "id", "de.se.ludoapp");
	}

	/**
	 * @param type
	 * @return the playerimage of the specific player color
	 */
	public Drawable getPlayerImageByType(PlayerType type) {
		Drawable imageSelectedPlayer = null;
		switch (type) {
		case PLAYER_YELLOW:
			imageSelectedPlayer = getResources().getDrawable(
					R.drawable.yellow_player);
			break;
		case PLAYER_RED:
			imageSelectedPlayer = getResources().getDrawable(
					R.drawable.red_player);

			break;
		case PLAYER_GREEN:
			imageSelectedPlayer = getResources().getDrawable(
					R.drawable.green_player);
			break;
		case PLAYER_BLUE:
			imageSelectedPlayer = getResources().getDrawable(
					R.drawable.blue_player);
			break;
		default:
			break;
		}
		return imageSelectedPlayer;
	}

	@Override
	public void onBackPressed() {
		// If you pushed backed button on your smartphone, it will be happen
		// nothing.
		// You have to leave the game active over a button in the game.
	}

}
